// Shader created with Shader Forge v1.40 
// Shader Forge (c) Freya Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.40;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,cgin:,cpap:True,lico:1,lgpr:1,limd:1,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,imps:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,atcv:False,rfrpo:True,rfrpn:Refraction,coma:15,ufog:True,aust:True,igpj:False,qofs:0,qpre:1,rntp:1,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:0.5,fgcg:0.5,fgcb:0.5,fgca:1,fgde:0.01,fgrn:0,fgrf:300,stcl:False,atwp:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False,fsmp:False;n:type:ShaderForge.SFN_Final,id:2672,x:32719,y:32712,varname:node_2672,prsc:2;pass:END;sub:END;*/

Shader "Unlit/CodeOldSchoolPlus" {
    Properties {
        _BaseColor ("BaseColor", Color) = (0.1132075,0.1121395,0.1121395,1)
        _LightColor ("LightColor", Color) = (1,1,1,1)
        _SpecPow ("高光次幂", Range(1,90)) = 30
        _Occlusion ("AO贴图", 2D) = "white" {}
        _EnvTopCol ("环境天顶颜色", Color) = (1,0.9716981,0.9716981,1)
        _EnvMidCol ("环境水平颜色", Color) = (0.5,0.5,0.5,1)
        _EnvBottomCol ("环境地表颜色", Color) = (0,0,0,1)
        _3ColInt ("环境光强度", Range(0, 1)) = 0
    }
    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        LOD 100
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"
            // 引入文件
            #include "AutoLight.cginc"                              // 投影必须依赖
            #include "Lighting.cginc"                               // 投影必须依赖
            #pragma multi_compile_fwdbase_fullshadows
            #pragma multi_compile_fog
            #pragma target 3.0

            // 输入参数
            uniform float3 _BaseColor;
            uniform float3 _LightColor;
            uniform float _SpecPow;
            uniform sampler2D _Occlusion;
            uniform float3 _EnvTopCol;
            uniform float3 _EnvMidCol;
            uniform float3 _EnvBottomCol;
            uniform float _3ColInt;

            struct VertexInput {
                float4 vertex : POSITION;                           // 顶点信息
                float4 normal : NORMAL;                             // 法线信息
                float2 uv0 : TEXCOORD0;                             // uv 坐标0通道
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;                         // 裁剪空间
                float2 uv0 : TEXCOORD0;                             // UV 信息
                float4 posWS : TEXTCOORD1;                          // 世界空间顶点信息
                float3 nDirWS : TEXTCOORD2;                         // 世界空间法线方向
                LIGHTING_COORDS(3, 4)                               // 投影坐标
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;                   // 创建输出结构
                o.pos = UnityObjectToClipPos( v.vertex );         // 变换顶点位置 OS > CS
                o.uv0 = v.uv0;                                      // 传递 UV
                o.posWS = mul(unity_ObjectToWorld, v.vertex);       // 变换顶点位置 OS > WS
                o.nDirWS = UnityObjectToWorldNormal(v.normal);      // 变换法线方向 OS > WS
                TRANSFER_VERTEX_TO_FRAGMENT(o)                      // 投影坐标
                return o;
            }

            // 输出结构 >> 像素
            float4 frag(VertexOutput i) : COLOR {
                // 准备向量
                float3 nDir = normalize(i.nDirWS);
                float3 lDir = _WorldSpaceLightPos0.xyz;
                float3 vDir = normalize(_WorldSpaceCameraPos.xyz - i.posWS.xyz);
                float3 rDir = reflect(-lDir, nDir);

                // 点积
                float nDotl = dot(nDir, lDir);
                float vDotr = dot(vDir, rDir);

                // 基本光照模型
                float shadow = LIGHT_ATTENUATION(i);
                float lambert = max(0.0, nDotl);
                float phong = pow(max(0.0, vDotr), _SpecPow);

                float3 dirLighting = (_BaseColor * lambert + phong) * _LightColor * shadow;

                // 计算环境光照
                float topMask = max(0.0, nDir.g);                   // 顶部光照
                float btmMask = max(0.0, -nDir.g);                  // 底部光照
                float midMask = 1.0 - topMask - btmMask;            // 侧面光照
                // 混合
                float3 envCol = _EnvTopCol * topMask + _EnvMidCol * midMask + _EnvBottomCol * btmMask;
                // 纹理采样
                float occlusion = tex2D (_Occlusion, i.uv0);
                float3 envLighting = _BaseColor * envCol * occlusion;


                return float4(envLighting, 1.0);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}
